package com.zerocarbon.framework.web.config;

import com.zerocarbon.framework.core.extension.IAppSecretUtil;
import com.zerocarbon.framework.web.aspect.ValidRestAspect;
import com.zerocarbon.framework.web.handler.IValidRestHandler;
import com.zerocarbon.framework.web.handler.ValidRestHandler;
import com.zerocarbon.framework.web.properties.RestProperties;
import com.zerocarbon.framework.web.utils.AppSecretUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 引入hico-framework-web-starter后生效。注入IAppSecretUtil、IValidRestHandler及ValidRestAspect切面
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({RestProperties.class})
public class RestAutoConfiguration {
	
	/**
	 * 注入IAppSecretUtil。appkey和appSecret的管理工具
	 * @return
	 */
    @Bean
    @ConditionalOnMissingBean(IAppSecretUtil.class)
    public IAppSecretUtil getAppSecretUtil() {
        return new AppSecretUtil();
    }

	/**
	 * 注入IValidRestHandler
	 * IValidRestHandler负责认证和签名
	 * @return
	 */
	@Bean
	@ConditionalOnMissingBean(IValidRestHandler.class)
//	@ConditionalOnProperty(name = "zerocarbon.rest.valid.type", havingValue = "IHandler", matchIfMissing = true)
	public IValidRestHandler getValidRestHandler() {
		log.info("--------IValidRestHandler(ValidRestHandler) init");
		return new ValidRestHandler();
	}
	
	/**
	 * 启动切面
	 * ValidRestAspect用于扫描Controller类的ValidRestAuth和ValidRestSign注解，再使用IValidRestHandler完成验证
	 * @return
	 */
	@Bean
//	@ConditionalOnProperty(name = "zerocarbon.rest.valid.enabled", havingValue = "true", matchIfMissing = true)
//	@ConditionalOnProperty(name = "zerocarbon.rest.valid.enabled", matchIfMissing = true)
    public ValidRestAspect getValidRestAspect(IValidRestHandler iValidRestHandler) {
		log.info("--------ValidRestAspect init");
        return new ValidRestAspect(iValidRestHandler);
    }
	
}
